<?php
/**
 * 日志
 * User: ws
 * Date: 2018/8/20
 * Time: 17:49
 */

namespace app\admin\controller;
use think\Controller;
use think\Db;
use app\admin\model\Log as logModel;
class Log extends Controller
{
    function index(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            $sj = input('sj');
            $da = explode(" - ", $sj);

            if($sj){
                $beginToday = strtotime($da[0]);
                $endToday = strtotime($da[1]);
                $where['create_time'] = array(array('GT',$beginToday),array('LT',$endToday)) ;
            }


            $where['title|url|ip|param'] = array('like','%'.trim($key).'%');
            $list = Db::name('log')->order('id desc')->where($where)->paginate(input('limit'))->toArray();

            foreach ($list['data'] as $key=>$item){
                if($item['type']==1){
                    $list['data'][$key]['a_name'] = Db::name('admin_user')->where("admin_user_id={$item['a_id']}")->field('name')->find();
                }else{
                    $list['data'][$key]['a_name'] = Db::name('store')->where("store_id={$item['a_id']}")->field('store_name')->find();
                }
                //$list['data'][$key]['param'] = json_decode($item['param']);
            }

            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }
        return $this->fetch('list');
    }

    function del(){
        $id = $_POST['id'];
        if(!logModel::destroy($id)){
            return json(array('code'=>0,'msg'=>'删除失败'));
        };
        return json(array('code'=>200,'msg'=>'删除成功'));
    }
}